/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package assignment2;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

/**
 * ProductSchedule Class:
 * This class is used to list the products available and schedule products for manufacturing
 * @author Vale
 *
 */
public class ProductSchedule implements ActionListener, WindowListener {

    private JFrame schedProdFrame;
    private JScrollPane table;
    private JPanel headingPanel;
    private JPanel buttonPanel;
    private ProductList products;
    private JButton doneButton;
    private JButton cancelButton;
    private JTable prodTable;
    private InventoryList inventory;
    private DefaultTableModel prodTableModel;
    private static final String HEADING_FONT = "Heading";
    private static final int HEADING_FONT_SIZE = 20;
    private static final String FRAME_HEADING = "Schedule Products for Manufacturing";
    private static final String GUI_TITLE = "Schedule Products";
    private static final String DONE_BUTTON = "Done";
    private static final String CANCEL_BUTTON = "Cancel";
    private static final int FRAME_WIDTH = 400;
    private static final int FRAME_HEIGHT = 500;
    private static final int BPANEL_ROWS = 1;
    private static final int BPANEL_COLS = 2;
    private static final int BUTTON_SPACING = 5;
    private static final int TABLE_COLS = 2;
    private static final String TABLE_HEAD_COL1 = "Product Name";
    private static final String TABLE_HEAD_COL2 = "Products Scheduled";
    private static final int ZERO = 0;
    private final String ERR = "Error";
    private final String INCORRECT_INPUT = "Invalid Number of Products";

    /**
     * ProductSchedule Constructor:
     * This is the constructor for the ProductSchedule class. It takes the product list as input and shows each product and the respective number of products scheduled, by setting up
     * the interface.
     * @param prodList The current product list
     *
     */
    public ProductSchedule(ProductList prodList, InventoryList invList) {
        //Create new Objects
        prodTableModel = new DefaultTableModel();
        products = prodList;
        inventory = invList;
        schedProdFrame = new JFrame(FRAME_HEADING);
        headingPanel = new JPanel();
        buttonPanel = new JPanel();
        doneButton = new JButton(DONE_BUTTON);
        cancelButton = new JButton(CANCEL_BUTTON);
        cancelButton.addActionListener(this);

        //Edit Frames properties
        schedProdFrame.addWindowListener(this);
        schedProdFrame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        schedProdFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        schedProdFrame.setLayout(new BorderLayout());

        //Format Heading
        JTextArea heading = new JTextArea(GUI_TITLE);
        Color bgColour = schedProdFrame.getBackground();
        heading.setBackground(bgColour);
        Font headerFont = new Font(HEADING_FONT, Font.BOLD, HEADING_FONT_SIZE);
        heading.setFont(headerFont);
        headingPanel.add(heading);

        //Add Buttons
        buttonPanel.setLayout(new GridLayout(BPANEL_ROWS, BPANEL_COLS, BUTTON_SPACING, BUTTON_SPACING));
        buttonPanel.add(doneButton);
        doneButton.addActionListener(new DoneButton());
        buttonPanel.add(cancelButton);

        //Print main Product table
        printProductTable();

        //Add content to frame
        schedProdFrame.getContentPane().add(headingPanel, BorderLayout.NORTH);
        schedProdFrame.getContentPane().add(table, BorderLayout.CENTER);
        schedProdFrame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        schedProdFrame.setVisible(true);
    }

    /**
     * DoneButton Inner Class:
     * This inner class is used as a listener to the "Done" button. It is used to confirm the changes made to the schedule.
     *
     */
    public class DoneButton implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            for (int i = ZERO; i < products.size(); i++) {
                try {
                    int prodSched = Integer.parseInt(prodTableModel.getValueAt(i, TABLE_COLS-1).toString());
                    products.get(i).setProdSched(prodSched);
                } catch (NumberFormatException x) {
                    JOptionPane.showMessageDialog(schedProdFrame, INCORRECT_INPUT, ERR, JOptionPane.ERROR_MESSAGE);
                    return;
                }
            }
            schedProdFrame.dispose();
            new SCMSGUI();
        }
    }

    /**
     * void actionPerformed(ActionEvent e) Method:
     * This is called when the "Cancel" button is pressed. It calls dispose(), which frees up all currently used resources by the current frame (schedProdFrame)
     * and consequently closes it.
     *
     */
    public void actionPerformed(ActionEvent e) {
        schedProdFrame.dispose();
    }

    /**
     * void windowOpened(WindowEvent e) Method:
     * This is one of the abstract methods of the WindowListener class. It has not been implemented.
     *
     */
    public void windowOpened(WindowEvent e) {
    }

    /**
     * void windowClosing(WindowEvent e) Method:
     * This is one of the abstract methods of the WindowListener class. It has been implemented such that if the frame is closed without using the specified "Back"
     * button, the resources used by the frame are released.
     *
     */
    public void windowClosing(WindowEvent e) {
        schedProdFrame.dispose();
    }

    /**
     * void windowClosed(WindowEvent e) Method:
     * This is one of the abstract methods of the WindowListener class. It has not been implemented, as its purpose has been covered by the 'windowClosing' event.
     *
     */
    public void windowClosed(WindowEvent e) {
    }

    /**
     * void windowIconified(WindowEvent e) Method:
     * This is one of the abstract methods of the WindowListener class. It has not been implemented.
     *
     */
    public void windowIconified(WindowEvent e) {
    }

    /**
     * void windowDeiconified(WindowEvent e) Method:
     * This is one of the abstract methods of the WindowListener class. It has not been implemented.
     *
     */
    public void windowDeiconified(WindowEvent e) {
    }

    /**
     * void windowActivated(WindowEvent e) Method:
     * This is one of the abstract methods of the WindowListener class. It has not been implemented.
     *
     */
    public void windowActivated(WindowEvent e) {
    }

    /**
     * void windowDeactivated(WindowEvent e) Method:
     * This is one of the abstract methods of the WindowListener class. It has not been implemented.
     *
     */
    public void windowDeactivated(WindowEvent e) {
    }

    /**
     * void printProductTable() Method:
     * This method is used to print the table of Products, and the data relavent to each. It also disables editing of certain cells.
     *
     */
    private void printProductTable() {
        int tableSize = products.getProdList().size();
        Object[][] rowData = new Object[tableSize][TABLE_COLS];
        Object[] columnNames = {TABLE_HEAD_COL1, TABLE_HEAD_COL2};
        for (int i = ZERO; i < tableSize; i++) {
            Object[] rowNum = {products.getProdList().get(i).getProdName(), products.getProdList().get(i).getProdSched()};
            rowData[i] = rowNum;
        }
        prodTable = new JTable(prodTableModel) {

            public boolean isCellEditable(int rowIndex, int colIndex) {
                if (colIndex == ZERO) {
                    return false;
                } else {
                    return true;
                }
            }
        };
        prodTableModel.setDataVector(rowData, columnNames);
        prodTable.setRowSelectionAllowed(true);
        table = new JScrollPane(prodTable);

    }
}
